#include <iostream>
#include <vector>
#include <algorithm>
#include "GeometryTypes.h"
#include "StrategyParser.h"
#include "ExecutionEngine.h"

int main() {
    // 1. 定义策略
    std::vector<Strategy> strategies = {
        {{"O", "C"}, "将点O放置在坐标系原点(0,0)，根据OC=4将点C放置在(4,0)位置"},
        {{"O"}, "以点O为圆心，半径5绘制圆O"},
        {{"AB", "C"}, "过点C作圆O的切线AB，使AB垂直于OC"},
        {{"OC"}, "连接点O和点C形成线段OC，长度为4"},
        {{"AB", "O"}, "弦AB与圆O相交于两点，且关于OC对称"}
    };
    
    // 2. 解析策略
    StrategyParser parser;
    std::vector<std::pair<Strategy, AnalysisResult>> analyzedStrategies;
    
    std::cout << "策略解析结果:\n";
    std::cout << "==================================================\n";
    
    for (const auto& strategy : strategies) {
        AnalysisResult analysis = parser.parseStrategy(strategy);
        analyzedStrategies.push_back({strategy, analysis});
        
        std::cout << "策略: " << strategy.strategy << std::endl;
        std::cout << "→ 操作类型: " << analysis.operationType << std::endl;
        std::cout << "→ 计算方法: " << analysis.calculationMethod << std::endl;
        std::cout << "→ 执行顺序: " << analysis.executionOrder << std::endl;
        std::cout << std::endl;

        // 调试信息：显示提取的参数
        if (analysis.parameters.find("distance_constraint") != analysis.parameters.end()) {
            auto constraint = std::any_cast<std::map<std::string, std::any>>(analysis.parameters.at("distance_constraint"));
            std::cout << "→ 距离约束: " << std::any_cast<std::string>(constraint.at("segment")) 
                        << "=" << std::any_cast<double>(constraint.at("length")) << std::endl;
        }
        if (analysis.parameters.find("radius") != analysis.parameters.end()) {
            std::cout << "→ 半径: " << std::any_cast<double>(analysis.parameters.at("radius")) << std::endl;
        }
        std::cout << std::endl;
    }

    
    // 3. 按执行顺序排序
    std::sort(analyzedStrategies.begin(), analyzedStrategies.end(),
              [](const auto& a, const auto& b) {
                  return a.second.executionOrder < b.second.executionOrder;
              });
    
    // 4. 执行计算
    ExecutionEngine engine;
    GeometryState finalState = engine.executeAnalyzedStrategies(analyzedStrategies);
    
    // 5. 输出最终结果
    std::cout << "\n最终几何状态:\n";
    std::cout << "点坐标:\n";
    for (const auto& [name, point] : finalState.points) {
        std::cout << "  " << name << ": (" << point.x << ", " << point.y << ")\n";
    }
    
    std::cout << "\n线段:\n";
    for (const auto& [name, segment] : finalState.segments) {
        std::cout << "  " << name << ": ";
        for (const auto& pointName : segment.points) {
            auto point = finalState.points[pointName];
            std::cout << pointName << "(" << point.x << "," << point.y << ") ";
        }
        std::cout << "长度=" << segment.length << "\n";
    }
    
    std::cout << "\n圆:\n";
    for (const auto& [name, circle] : finalState.circles) {
        std::cout << "  " << name << ": 圆心(" << circle.center.x << "," << circle.center.y 
                  << "), 半径=" << circle.radius << "\n";
    }
    
    std::cout << "\n直线:\n";
    for (const auto& [name, line] : finalState.lines) {
        std::cout << "  " << name << ": " << line.description << "\n";
    }
    
    return 0;
}